package day20180131.designMode.fifteen_Stategy;

/**
 * Created by 14394 on 2018/1/31.
 */

//辅助类
public abstract class AbstractCalculator {

    public int[] split(String exp,String opt){
        String[] arrayStr = exp.split(opt);
        int[] arrayInt = new int[2];

        arrayInt[0] = Integer.parseInt(arrayStr[0]);
        arrayInt[1] = Integer.parseInt(arrayStr[1]);

        return arrayInt;
    }
}

//算法接口
interface ICalculator{
    int calculate(String str);


    /*使用场景：   对外提供算法可以自由切换的策略
    *             避免使用多种条件判断
    *             扩展性好，再现有策略上增加新策略不需要改变原有方法
    * */
    public static void main(String[] args) {
        ICalculator iCalculator = new Plus();
        System.out.println(iCalculator.calculate("1+1"));

        iCalculator = new Minus();
        System.out.println(iCalculator.calculate("1-1"));
    }
}

//减法
class Minus extends AbstractCalculator implements ICalculator{

    @Override
    public int calculate(String str) {
        int[] arrayInt = split(str,"\\-");

        return arrayInt[0] - arrayInt[1];
    }
}
//加法
class Plus extends AbstractCalculator implements ICalculator{

    @Override
    public int calculate(String str) {
        int[] arrayInt = split(str,"\\+");

        return arrayInt[0] + arrayInt[1];
    }
}